package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @PostMapping("add")
    public Result add(@RequestBody Category category) {
        return categoryService.add(category);
    }

    @GetMapping("list")
    public Result<List<Category>> list() {
        return categoryService.getList();
    }

    @GetMapping("detail")
    public Result<Category> detail(Integer id) {
        return categoryService.findById(id);
    }

    @PutMapping("update")
    public Result update(@RequestBody @Validated Category category) {
        category.setUpdateTime(LocalDateTime.now());
        boolean update = categoryService.updateById(category);
        return Result.success(update);
    }
}
